package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.dto.input.*;
import com.b511.swpu.cementbackend.service.DataQueryService;
import com.b511.swpu.cementbackend.service.ReportGenerateService;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "数据查询", tags = {"数据查询"})
@RestController
@RequestMapping("/dataQuery")
public class DataQueryController {

    private final DataQueryService dataQueryService;

    private final ReportGenerateService reportGenerateService;

    public DataQueryController( DataQueryService dataQueryService, ReportGenerateService reportGenerateService) {
        this.dataQueryService = dataQueryService;
        this.reportGenerateService = reportGenerateService;
    }

    @GetMapping("/{num}/{size}")
    public R getWorkByCondition(@PathVariable int num, @PathVariable int size, @ModelAttribute WorkQueryDto workQueryDto) throws Exception {
        return dataQueryService.getWorkByCondition(workQueryDto, PageRequest.of(num, size));
    }

    //预览和下载文件
    @GetMapping("/preview/{filePath}")
    public R filePreview(@PathVariable String filePath, HttpServletResponse response){
        return dataQueryService.filePreview(filePath,response);
    }

    //通过井名查询该井有哪些文件
    @GetMapping("/{wellName}")
    public R getWellFile(@PathVariable String wellName){
        return dataQueryService.getWellFile(wellName);
    }

    // 通过井名查询 CM_WorkBaseInfo 该接口返回数据进行了封装
    @GetMapping("/base/{wellName}")
    public R getWorkBaseInfo(@PathVariable String wellName){
        return dataQueryService.getWorkBaseInfo(wellName);
    }

    // 通过井名查询并返回全部套管的信息
    @GetMapping("/casings/{wellName}")
    public R getCasingsData(@PathVariable String wellName){
        return dataQueryService.getCasingsData(wellName);
    }

    @GetMapping("/compare")
    public R getCompareData(@ModelAttribute CasingCompareInputDto compareInDto){
        return dataQueryService.getCasingCompare(compareInDto);
    }

    // 返回套管对比分析的树状结构
    @GetMapping("/compareTree")
    public R compareTree(){
        return dataQueryService.getCompareTree();
    }

    @GetMapping("/compareTreeToCasingSize")
    public R compareTreeToCasingSize(){
        return dataQueryService.getCompareTreeCasingSize();
    }

    //返回井浏览的树形json
    @GetMapping("/wellBrowse")
    public R wellBrowse(HttpServletRequest request){
        return dataQueryService.wellBrowse(request);
    }

    //当前登陆人的作业公司及所有的作业公司
    @GetMapping("/loginCompany")
    public R loginCompany(HttpServletRequest request){
        return dataQueryService.loginCompany(request);
    }

    //根据id返回该井具体的数据
    @GetMapping("/wellBrowse/{cm_WorkBaseInfo_ID}")
    public R wellInfoDetail(@PathVariable String cm_WorkBaseInfo_ID){
        return dataQueryService.wellInfoDetail(cm_WorkBaseInfo_ID);
    }

    //根据公司名返回该公司下的树形结构
    @GetMapping("/wellBrowse/company/{company}")
    public R getTreeByCompany(@PathVariable String company){
        return dataQueryService.getTreeByCompany(company);
    }

    //上传文件，可以批量上传
    @PostMapping("/upload")
    public R upload(@RequestParam(value = "file",required = false)  MultipartFile[] multipartFiles,@RequestParam(value = "wellName",required = false) String wellName ){
        return dataQueryService.upload(multipartFiles,wellName);
    }

    //返回文件浏览的树状结构（没有套管那层）
    @GetMapping("/wellDocTree/{company}")
    public R wellDocTree(@PathVariable String company){
        return dataQueryService.wellDocTree(company);
    }

    //新建文件夹
    @GetMapping("/createNewFile/{fileName}")
    public R createNewFile(@PathVariable String fileName){
        return dataQueryService.createNewFile(fileName);
    }

    //删除文件
    @DeleteMapping("/deleteFile")
    @Transactional
    public R deleteFile(@RequestBody DeleteFileDto deleteFileDto){
        return dataQueryService.deleteFile(deleteFileDto.getFilePath());
    }

    //执行select语句
    @PostMapping("/execQuery")
    public R execQuery(@RequestBody SqlDto sqlDto) throws Exception {
        return dataQueryService.execQuery(sqlDto);
    }

    //执行insert、delete、create语句
    @PostMapping("/execUpdate")
    public R execUpdate(@RequestBody SqlDto sqlDto) throws Exception {
        return dataQueryService.execUpdate(sqlDto);
    }

    // TODO: 固井作业报表生成相关接口
    // 通过wellName查下面的全部尺寸 -- 返回List<>
    @GetMapping("/casingSize")
    public R getCasingSizeByWellName(@RequestParam(value = "wellName") String wellName ){
        return reportGenerateService.getCasingSizeListByWellName(wellName);
    }

    // TODO: 固井作业报表生成相关接口
    // 通过wellName查下面的全部尺寸 -- 返回List<>
    @GetMapping("/casingSize_wk")
    public R getWorkCasingSizeByWellName(@RequestParam(value = "wellName") String wellName ){
        return reportGenerateService.getCasingSizeListByWellNameFromWorkData(wellName);
    }
    // 通过 wellName, casingSize 返回 报表数据
    @GetMapping("/report")
    public R getReportByWellNameAndCasingSize(@RequestParam(value = "wellName") String wellName,
                                              @RequestParam(value = "casingSize") String casingSize){
        return reportGenerateService.getReportByWellNameAndCasingSize(wellName, casingSize);
    }

    // TODO: 固井综合数据报表生成相关接口
    // 通过 wellName 和 casingSize 返回固井综合数据报表
    @PostMapping("/generalDataReport")
    public R getGeneralDataReportByWellNameAndCasingSize(@RequestBody List<ReportBaseDto> inputDto){
        return reportGenerateService.getGeneralDataReportByWellNameAndCasingSize(inputDto);
    }


}
